using System;
using System.Collections.Generic;
using System.Text;
using Exceptions;
using RJB.S3.Entities;

namespace RJB.S3Library
{
    public class FolderManager : Manager, IFolderManager
    {

        public FolderManager(string UserKey, string SecretKey) : base(UserKey, SecretKey)
        {
        }
        /// <summary>
        /// Creates a folder for the user's S3 Account
        /// </summary>
        /// <param name="FoldertoSave">The Folder to save</param>
        public void CreateFolder(Folder FoldertoSave)
        {
            AmazonS3 _s3 = new AmazonS3();
            DateTime _timestamp = CurrentTimeStamp();

            try
            {
                _s3.CreateBucket(FoldertoSave.Name, null, this.UserKey, _timestamp, true, this.GenerateSignature(AmazonMethods.CreateBucket, _timestamp));
            }
            catch
            {
                throw new FolderSaveException("Folder Creation failed");
            }
            
        }
        /// <summary>
        /// Deletes a folder (bucket) on the S3 service.
        /// </summary>
        /// <param name="FolderName"></param>
        public void DeleteFolder(string FolderName)
        {
            AmazonS3 _s3 = new AmazonS3();
            DateTime _timestamp = CurrentTimeStamp();

            Status _status = _s3.DeleteBucket(FolderName, this.UserKey, _timestamp, true, GenerateSignature(AmazonMethods.DeleteBucket, _timestamp), null);
            if (_status.Code != 204)
                throw new FolderDeleteException("Error deleting Folder.  The error returned was: " + _status.Description);
        }
        /// <summary>
        /// This method deletes all the files (objects) in a folder (bucket)
        /// </summary>
        /// <param name="FolderName">The Foldername to delete the files.</param>
        public void DeleteFolderRecursive(string FolderName)
        {
            try
            {
                FileManager _fm = new FileManager(this.UserKey, this.SecretKey);
                List<File> _files = _fm.ListFiles(FolderName);
                foreach (File file in _files)
                    _fm.DeleteFile(file.Name, FolderName);
                
                this.DeleteFolder(FolderName);
            }
            catch
            {
                throw new FolderDeleteException("Recursive Deletion Failed.");
            }
        }
        /// <summary>
        /// Retrieves a list of the folders (buckets) for 
        /// the user's S3 account.
        /// </summary>
        /// <returns>A generic list of folders</returns>
        public List<Folder> List()
        {
            AmazonS3 _s3 = new AmazonS3();
            DateTime _timestamp = CurrentTimeStamp();

            ListAllMyBucketsResult _result = _s3.ListAllMyBuckets(this.UserKey, _timestamp, true, GenerateSignature(AmazonMethods.ListAllMyBuckets, _timestamp));

            List<Folder> _directories = new List<Folder>(_result.Buckets.Length);
            
            foreach (ListAllMyBucketsEntry entry in _result.Buckets)
                _directories.Add(new Folder(entry.Name, entry.CreationDate, _result.Owner.DisplayName));

            return _directories;
        }
    }
}
